package com.overoid.mynote;

import java.util.List;

import com.overoid.mynote.data.DBHelper.MemoClass;
import com.overoid.mynote.R;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MemoListAdapter extends BaseAdapter {
	private static final String CLASSNAME = MemoListAdapter.class.getSimpleName();
	private final Context context;
	private final List<MemoClass> mc;
	
	public MemoListAdapter(Context context, List<MemoClass> mc) {
		this.context = context;
		this.mc = mc;
		Log.v(Constants.LOGTAG, MemoListAdapter.CLASSNAME + " - constructor()");
	}

	@Override
	public int getCount() {
		
		return this.mc.size();
	}

	@Override
	public Object getItem(int position) {
		
		return this.mc.get(position);
	}

	@Override
	public long getItemId(int position) {

		//return this.mc.get(position).memoClasId;
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		if(convertView == null) {
			MemoClass mc = this.mc.get(position);
			return new MemoListView(this.context, mc.memoClassBackgroudImage, mc.memoClassName, mc.memoCount);
		}
		else {
			return convertView;
		}
		 

	}
	
	private final class MemoListView extends LinearLayout {
		private ImageView folderIcon;
		private TextView  memoClassName;
		
		public MemoListView(Context context, String memoClassBackgroudImage, String memoClassName, long memoCount) {
			super(context);
			this.setOrientation(HORIZONTAL);
			Log.v(Constants.LOGTAG,MemoListAdapter.CLASSNAME + " - MemoListView Constructor()");
			
			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
					ViewGroup.LayoutParams.WRAP_CONTENT);
			params.setMargins(5, 2, 5, 2);
			
			
			this.folderIcon = new ImageView(context);
			this.folderIcon.setImageResource(getResourceId(memoClassBackgroudImage));
			Log.v(Constants.LOGTAG, MemoListAdapter.CLASSNAME + " ImageName:" + memoClassBackgroudImage);
			Log.v(Constants.LOGTAG, MemoListAdapter.CLASSNAME + " ImageID:" + getResourceId(memoClassBackgroudImage));
			//this.folderIcon.setImageResource(R.drawable.ico_folder_default);
			this.folderIcon.setPadding(5,1,5,1);
			this.addView(this.folderIcon, params);
			
			
			
			this.memoClassName = new TextView(context);
			this.memoClassName.setText(memoClassName + "(" + String.valueOf(memoCount) + ")");
			this.memoClassName.setTextSize(20);
			this.memoClassName.setTextColor(Color.BLACK);
			this.memoClassName.setGravity(Gravity.CENTER_VERTICAL);
			this.addView(this.memoClassName,params);			
			
			Log.v(Constants.LOGTAG, MemoListAdapter.CLASSNAME + " memoClass:" + memoClassName + "(" + String.valueOf(memoCount) + ")");
		}
	}
	
	private int getResourceId(String memoClassBackgroudImage) {
		int resourceId = R.drawable.ico_folder_default;
		
		if(memoClassBackgroudImage.equals("ico_folder_black")) {
			resourceId = R.drawable.ico_folder_black;
		} 
		else if(memoClassBackgroudImage.equals("ico_folder_blue")) {
			resourceId = R.drawable.ico_folder_blue;
		}
		else if(memoClassBackgroudImage.equals("ico_folder_brown")) {
			resourceId = R.drawable.ico_folder_brown;
		}
		else if(memoClassBackgroudImage.equals("ico_folder_green")) {
			resourceId = R.drawable.ico_folder_green;
		}
		else if(memoClassBackgroudImage.equals("ico_folder_orange")) {
			resourceId = R.drawable.ico_folder_orange;
		}
		else if(memoClassBackgroudImage.equals("ico_folder_red")) {
			resourceId = R.drawable.ico_folder_red;
		}
		else if(memoClassBackgroudImage.equals("ico_folder_violet")) {
			resourceId = R.drawable.ico_folder_violet;
		}
		else if(memoClassBackgroudImage.equals("ico_folder_white")) {
			resourceId = R.drawable.ico_folder_white;
		}
		else if(memoClassBackgroudImage.equals("ico_folder_yellow")) {
			resourceId = R.drawable.ico_folder_yellow;
		}

		return resourceId;
	}
	
	
}
